package com.yunji.cabinet.service.impl;

import com.google.common.collect.Maps;
import com.yunji.cabinet.dao.IDeviceEventDao;
import com.yunji.cabinet.device.DeviceCommandEngine;
import com.yunji.cabinet.device.factory.DeviceStrategyFactory;
import com.yunji.cabinet.domain.DeviceEventDO;
import com.yunji.cabinet.params.DeviceEventParam;
import com.yunji.common.utils.bean.BeanUtils;
import com.yunji.coffee.entity.bo.CommonDeviceInfoBO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author : peter-zhu
 * @date : 2025/1/17 17:46
 * @description : TODO
 */
@Service
public class DeviceService {

    @Resource
    IDeviceEventDao cabinetRecordsDao;

    /**
     * @return List<CabinetRecords> 操作日志记录列表
     * @Title search
     * @Desc 查询操作日志记录列表
     * @Date 2024-04-13 14:11:21
     * @Param records 操作日志记录
     */
    public List<DeviceEventDO> search(DeviceEventParam param) {
        return cabinetRecordsDao.search(param);
    }

    public CommonDeviceInfoBO getDeviceInfoBO(String eventDataCode) {
        return getDeviceInfoBO(eventDataCode, null);
    }

    public CommonDeviceInfoBO getDeviceInfoBO(String eventDataCode, Integer finishStatus) {
        DeviceEventDO data = cabinetRecordsDao.getByEventDataCode(eventDataCode, finishStatus);
        if (BeanUtils.isNull(data)) return null;
        return DeviceCommandEngine.getDeviceInfo(data, data.getContainerType());
    }

    public Map<String, CommonDeviceInfoBO> getDeviceInfoBOMap(List<String> eventDataCodeList) {
        List<DeviceEventDO> dataList = cabinetRecordsDao.listByEventDataCode(eventDataCodeList);
        Map<String, CommonDeviceInfoBO> maps = Maps.newHashMap();
        for (DeviceEventDO dto : dataList) {
            maps.put(dto.getEventDataCode(), DeviceStrategyFactory.getStrategy(dto.getContainerType()).getDeviceInfo(dto));
        }
        return maps;
    }

    /**
     * @return boolean 新增结果
     * @Title create
     * @Desc 新增操作日志记录
     * @Date 2024-04-13 14:11:46
     * @Param containerRecords 操作日志记录
     */
    public boolean createEvent(DeviceEventDO records) {
        return cabinetRecordsDao.save(records);
    }

    /**
     * @param param 过滤信息
     * @Title searchTableField
     * @Desc 匹配表字段信息
     * @Date 2024-09-09 14:56:41.314
     * @Return List<String> 匹配结果
     */
    public List<String> searchTableField(DeviceEventParam param) {
        return cabinetRecordsDao.searchTableField(param);
    }

    public DeviceEventDO getEventById(Long eventId) {
        return cabinetRecordsDao.getById(eventId);
    }

    public void updateEvent(String maintainCode, String afterCmd) {
        cabinetRecordsDao.updateEvent(maintainCode,afterCmd);
    }
}
